package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_bankcard;
import cn.com.api.service.ApiT_bankcardService;
import cn.com.api.service.CheckService;
import cn.com.pingpp.PingppUtil;
import cn.com.util.*;

/**
 * ClassName: ApiT_bankcardController
 * 
 * @Description: 银行卡模块控制器
 * @author yewl
 * @date 2017年3月6日
 */
@Controller
@RequestMapping(Utils.SYS_API + "bankcard/")
public class ApiT_bankcardController {
	@Autowired
	private ApiT_bankcardService apiT_bankcardService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiT_bankcardController.class);

//	/**
//	 * @Title: identifyBankCard
//	 * @Description:验证银行卡
//	 * @param user_id
//	 * @param token
//	 * @param id_name
//	 *            身份证姓名
//	 * @param id_number
//	 *            身份证号码
//	 * @param card_number
//	 *            银行卡号码
//	 * @param phone_number
//	 *            手机号
//	 * @return void
//	 */
//	@RequestMapping(value = "identifyBankCard")
//	public void identifyBankCard(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
//			String id_name, String id_number, String card_number, String phone_number) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		try {
//			Map<String, String> paramMap = new HashMap<String, String>();
//			paramMap.put("u_id", user_id);
//			paramMap.put("token", token);
//			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
//				map.clear();
//				paramMap.clear();
//				Integer result = PingppUtil.identifyBankCard(id_name, id_number, card_number);
//				if (result == 1) {
//					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
//					map.put(Utils.MESSAGE, "验证成功");
//				} else {
//					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
//					map.put(Utils.MESSAGE, "验证失败");
//				}
//			}
//		} catch (Exception e) {
//			logger.error(e);
//			e.printStackTrace();
//			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
//			map.put(Utils.MESSAGE, "系统异常！");
//		}
//		JsonUtil.printJson(response, JsonUtil.object2json(map));
//	}

	/**
	 * @Title: getmybankcards
	 * @Description: 获取我的银行卡列表
	 * @param user_id
	 * @param token
	 * @return void
	 */
	@RequestMapping(value = "getmybankcards")
	public void getmybankcards(HttpServletRequest request, HttpServletResponse response, String user_id, String token) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				paramMap.clear();
				List<ApiT_bankcard> list = apiT_bankcardService.getmybankcards(user_id);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: addbankcard
	 * @Description: 添加银行卡
	 * @param user_id
	 * @param token
	 * @return void
	 */
	@RequestMapping(value = "addbankcard")
	public void addbankcard(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiT_bankcard apiT_bankcard, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				apiT_bankcard.setU_id(user_id);
				map = apiT_bankcardService.addbankcard(apiT_bankcard, password);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: delbankcard
	 * @Description: 删除银行卡
	 * @param user_id
	 * @param token
	 * @return void
	 */
	@RequestMapping(value = "delbankcard")
	public void delbankcard(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			Integer bankcardid) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				paramMap.clear();
				apiT_bankcardService.deleteT_bankcard(bankcardid);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title:getBankcardInfo
	 * @Description: 获取银行卡信息接口
	 * @param user_id
	 * @param token
	 * @return void
	 */
	@RequestMapping(value = "getBankcardInfo")
	public void getBankcardInfo(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String cardno) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiT_bankcardService.getBankcardInfo(cardno);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
